
package com.sm.shoplook.controller.suit;

import com.github.pagehelper.PageInfo;
import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.domain.suit.Suit;
import com.sm.shoplook.service.suit.SuitService;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.suit.back.req.SuitSearchReqVo;
import com.sm.shoplook.vo.suit.front.resp.SuitPageRespVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author chenpy
 * @desc 套装（系统后台）
 * @date 2018/9/5 11:44
 * @see
 */
@Controller
@RequestMapping("/suit")
public class SuitController {

	private static final Logger LOGGER = LoggerFactory.getLogger(SuitController.class);
	@Autowired
	private SuitService suitService;

	@RequestMapping(value = "/manager", method = RequestMethod.GET)
	public String manager() {
		return "/suit/suit_manager";
	}

	/**
	 * @Author chenpy
	 * @Date 2018/8/18 10:29
	 * @Description 套装列表
	 * @param suitSearchReqVo 参数对象
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "selectSuitListPage", method = RequestMethod.POST)
	public ApiResult<Map<String, Object>> selectSuitListPage(SuitSearchReqVo suitSearchReqVo) {
		HashMap<String, Object> map = new HashMap<>();
		ApiResult<PageInfo<SuitPageRespVo>> apiResult = suitService.selectSuitListPage(suitSearchReqVo);
		PageInfo<SuitPageRespVo> pageInfo = apiResult.getData();
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, map);
	}

	/**
	 * @Author chenpy
	 * @Date 2018/8/18 11:27
	 * @Description 按ID查询
	 * @param suitId
	 * @return
	 */
	@RequestMapping(value = "/info_suit/{suitId}/{type}", method = RequestMethod.GET)
	public String selectSuitById(@PathVariable("suitId") Integer suitId, Model model, @PathVariable("type") Integer type) {
		ApiResult<SuitPageRespVo> suitPageRespVoApiResult = suitService.selectSuitById(suitId);
		SuitPageRespVo data = suitPageRespVoApiResult.getData();
		if (data == null) {
			data = new SuitPageRespVo();
		}
		model.addAttribute("type", type);
		model.addAttribute("suit", data);
		return "/suit/info_suit";
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return
	 * @throws ApiException
	 */
	@ResponseBody
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public ApiResult<Integer> batchDelete(String[] ids) {
		try {
			return suitService.batchDelete(ids);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult<>(ResultMessage.RESULT_ERROR_0);
		}
	}

    /**
     * 套装审核
     * @param suit
     * @return
     * @throws ApiException
     */
    @ResponseBody
    @RequestMapping(value = "/updateAuditSuit", method = RequestMethod.POST)
    public ApiResult<Integer> updateAuditSuit(Suit suit) {
        try {
            return suitService.updateAuditSuit(suit);
        } catch (ApiException e) {
            e.printStackTrace();
            LOGGER.error("e: {}", e);
            return new ApiResult<>(ResultMessage.RESULT_ERROR_0);
        }
    }
}
